
//
//  GQLocationCityController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/6/9.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQLocationCityController.h"
#import "GQCity.h"
#import "GQProvince.h"
#import "GQRegisterAreaInfo.h"
#import "GQRegisterInfo.h"
#import "GQRegisterDetailController.h"


@interface GQLocationCityController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSArray *cities;

@end

@implementation GQLocationCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.width, self.view.height) style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kFlag];
    [self.view addSubview:tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.cities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFlag];
    GQCity *city = self.cities[indexPath.row];
    cell.textLabel.text = city.cityValue;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.text = city.cityCode;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GQCity *city = self.cities[indexPath.row];
    [GQRegisterInfo sharedGQRegisterInfo].location = city.cityValue;
    [GQRegisterInfo sharedGQRegisterInfo].addrCode = city.cityCode;
    GQProvince *province = [GQRegisterAreaInfo sharedGQRegisterAreaInfo].areaArr[_selectedRow];
    [GQRegisterInfo sharedGQRegisterInfo].prov = province.provinceValue;
    
    for (UIViewController *VC in self.navigationController.viewControllers) {
        if ([VC isKindOfClass:[GQRegisterDetailController class]]) {
            [self.navigationController popToViewController:VC animated:YES];
        }
    }
    
    
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"全部";
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 36;
}

- (NSArray *)cities
{
    if (_cities == nil) {
        GQProvince *province = [GQRegisterAreaInfo sharedGQRegisterAreaInfo].areaArr[_selectedRow];
        _cities = province.cities;
    }
    return _cities;
}

@end
